﻿using BBICMS.Infrastructure.Configs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;

namespace BBICMS.Infrastructure.Email
{
    public class SMTPService : IEmailService
    {
        public void SendMail(string mailto, string mailcc, string subject, string body)
        {
            MailMessage message = new MailMessage();
            if (string.IsNullOrEmpty(mailto))
                throw new ArgumentException("还没有收件邮箱，请在后台公司资料里填写邮箱地址！");

            message.To.Add(mailto);
            if(!string.IsNullOrEmpty(mailcc))
            {
                message.CC.Add(mailcc);
            }
           
            message.Subject = subject;
            message.Body = body; //string.Format("<p>{0}</p><p>发件人：{1} ({2}), 发件人邮箱：{3}</p>", body, name, phone, from);
            message.IsBodyHtml = true;

            //   SmtpClient smtp = new SmtpClient();
            SmtpClient smtpClient = new SmtpClient(SettingsManager.SMTP.SmtpServer, SettingsManager.SMTP.Port);

            smtpClient.UseDefaultCredentials = true;
            smtpClient.EnableSsl = SettingsManager.SMTP.EnableSsl;
            //   smtpClient.Port = SettingsManager.SMTP.Port;
            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtpClient.Credentials = new NetworkCredential(SettingsManager.SMTP.UserName, SettingsManager.SMTP.Password);

            smtpClient.Send(message);
        }
    }

}
